package com.ruoyi.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.service.IWsdMealsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.mealDishesDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdMeals;
import com.ruoyi.common.core.domain.vo.DailyMealPlanVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 *
 * 餐饮时间表 前端控制器
 *
 * @author wwq
 * @since 2024-04-20
 */
@RestController
@RequestMapping("/wsd_meals")
public class WsdMealsController extends BaseController {


    @Autowired
    private IWsdMealsService wsdMealsService;

    /**
     * 新增时间菜品信息
     */
    @PreAuthorize("@ss.hasPermi('business:meals:add')")
    @Log(title = "新增时间菜品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody mealDishesDTO mealDishesDto) {
        String dateString = mealDishesDto.getDate();
        LocalDate date = LocalDate.parse(dateString);  // 默认格式是 yyyy-MM-dd
        //查询数据库是否有这个日期的数据
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        LambdaQueryWrapper<WsdMeals> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WsdMeals::getDate,date);
        wrapper.eq(WsdMeals::getSchoolId,schoolId);
        Long count = wsdMealsService.count(wrapper);
        if (count != null && count != 0){
            return AjaxResult.error("这个时间已经配置");
        }
        return AjaxResult.success(wsdMealsService.insertWsdMeals(mealDishesDto));
    }


    /**
     * 查询学校食谱列表
     */
    @PreAuthorize("@ss.hasPermi('business:meals:list')")
    @GetMapping("/list")
    public TableDataInfo list( @RequestParam(required = false) String startDate,
                               @RequestParam(required = false) String endDate) {
        startPage();
        List<DailyMealPlanVO> list = wsdMealsService.selectWsdMealsList(startDate,endDate);
        return getDataTable(list);
    }


    /**
     * 修改学校食谱
     */
    @PreAuthorize("@ss.hasPermi('business:meals:edit')")
    @Log(title = "修改食谱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody mealDishesDTO mealDishesDto)
    {
       return AjaxResult.success(wsdMealsService.updateWsdMeals(mealDishesDto));
    }

    /**
     * 删除学校食谱
     */
    @PreAuthorize("@ss.hasPermi('business:meals:remove')")
    @Log(title = "删除学校食谱", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteSchoolRecipes")
    public AjaxResult remove(@RequestParam(required = false) String specificDate) {
        //获取学校id
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
       return toAjax(wsdMealsService.deleteWsdMealsByIds(specificDate,schoolId));
    }

    /**
     * 获取食谱详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:meals:query')")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestParam(required = false) String specificDate) {
        //获取学校id
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        return success(wsdMealsService.selectWsdLeavesById(specificDate,schoolId));
    }
}
